package edu.hebtu.controller;

import edu.hebtu.mapper.impl.TimeSlotMapper;
import edu.hebtu.pojo.TimeSlot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Controller
public class TimeSlotController {

    private final TimeSlotMapper timeSlotMapper;

    @Autowired
    public TimeSlotController(TimeSlotMapper timeSlotMapper) {
        this.timeSlotMapper = timeSlotMapper;
    }

    @RequestMapping("/timeSlot_info")
    public String timeSlot_info(Model model) {
        List<TimeSlot> timeSlots = timeSlotMapper.findById(null);
        model.addAttribute("timeSlots", timeSlots);
        return "admin/table-time-slot";
    }

    @RequestMapping("/timeSlot_add")
    public String timeSlot_add(Model model) {
        List<TimeSlot> timeSlots = timeSlotMapper.findById(null);
        String startTime, endTime;
        if (timeSlots.size() > 0) {
            timeSlots.sort(Comparator.comparing(TimeSlot::getEndTime));
            TimeSlot timeSlot = timeSlots.get(timeSlots.size() - 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
            startTime = simpleDateFormat.format(timeSlot.getEndTime());
            endTime = simpleDateFormat.format(new Date(timeSlot.getEndTime().getTime() + 60000 * 45));
        } else {
            startTime = "7:00";
            endTime = "7:45";
        }
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);
        return "admin/add-time-slot";
    }

    @RequestMapping("/addTimeSlot")
    public String addTimeSlot(String startTimeStr, String endTimeStr, TimeSlot timeSlot, Model model) {

        String [] start = startTimeStr.split(":");
        String [] end = endTimeStr.split(":");
        Calendar now = Calendar.getInstance();
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        now.set(Calendar.HOUR_OF_DAY, Integer.parseInt(start[0]));
        now.set(Calendar.MINUTE, Integer.parseInt(start[1]));
        timeSlot.setStartTime(now.getTime());
        now.set(Calendar.HOUR_OF_DAY, Integer.parseInt(end[0]));
        now.set(Calendar.MINUTE, Integer.parseInt(end[1]));
        timeSlot.setEndTime(now.getTime());

        model.addAttribute("startTime", startTimeStr);
        model.addAttribute("endTime", endTimeStr);

        if (timeSlot.getStartTime().compareTo(timeSlot.getEndTime()) > 0) {
            model.addAttribute("msg", "错误！该时间段的开始时间大于结束时间！");
            return "admin/add-time-slot";
        }


        List<TimeSlot> timeSlots = timeSlotMapper.findById(null);
        timeSlots.sort(Comparator.comparing(TimeSlot::getStartTime));
        for (TimeSlot timeSlot1 : timeSlots) {
            Calendar startTime = Calendar.getInstance();
            Calendar endTime = Calendar.getInstance();
            startTime.setTime(timeSlot1.getStartTime());
            endTime.setTime(timeSlot1.getEndTime());
            startTime.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
            endTime.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
            if (startTime.getTime().compareTo(timeSlot.getStartTime()) < 0 && endTime.getTime().compareTo(timeSlot.getStartTime()) > 0) {
                model.addAttribute("msg", "错误！该时间段的部分或全部时间段已存在！");
                return "admin/add-time-slot";
            }
            if (startTime.getTime().compareTo(timeSlot.getEndTime()) < 0 && endTime.getTime().compareTo(timeSlot.getEndTime()) > 0) {
                model.addAttribute("msg", "错误！该时间段的部分或全部时间段已存在！");
                return "admin/add-time-slot";
            }
        }
        timeSlotMapper.add(timeSlot);
        return "redirect:/timeSlot_info";
    }

    @RequestMapping("/timeSlot_delete/{timeSlotId}")
    public String timeSlot_delete(@PathVariable("timeSlotId") String timeSlotId, Model model) {
        TimeSlot timeSlot = timeSlotMapper.findById(timeSlotId).get(0);
        String startTime, endTime;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        startTime = simpleDateFormat.format(timeSlot.getStartTime());
        endTime = simpleDateFormat.format(timeSlot.getEndTime());
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);
        model.addAttribute("timeSlot", timeSlot);
        return "admin/delete-time-slot";
    }

    @RequestMapping("/deleteTimeSlot")
    public String deleteTimeSlot(TimeSlot timeSlot) {
        timeSlotMapper.delete(timeSlot);
        return "redirect:/timeSlot_info";
    }

}
